import * as $exercise from '@/api/exercise'
import * as _app from '@/utils/app'
import {toggleElFromArr} from '@/utils/helper'

export const UPDATE_NAV = 'update_nav'
export const UPDATE_EXERCISE_SELECT_IDS = 'update_exercise_select_ids'
export const UPDATE_EXERCISES = 'update_exercises'
export const UPDATE_CARDS = 'update_cards'
export const UPDATE_SENTENCES = 'update_sentences'

const state = () => {
  return {
    nav: 'card',
    exerciseSelectIds: [],
    cards: [],
    sentences: [],
  }
}

const getters = {
  cards (state) {
    let exercises = state.cards.map(n => {
      let exercise = {
        id: n.id,
        estimatedCompleteTime: n.estimatedCompleteTime,
        unitId: n.unitId,
        picPath: _app.getCardPicPathFromExercise(n),
        audioPath: _app.getCardAudioPathFromExercise(n),
        text: _app.getCardTextFromExercise(n),
        checked: state.exerciseSelectIds.includes(n.id)
      }
      return exercise
    })
    return exercises
  },
  sentences (state) {
    let exercises = state.sentences.map(n => {
      let exercise = {
        id: n.id,
        estimatedCompleteTime: n.estimatedCompleteTime,
        unitId: n.unitId,
        audioPath: _app.getCardAudioPathFromExercise(n),
        text: _app.getCardTextFromExercise(n),
        checked: state.exerciseSelectIds.includes(n.id)
      }
      return exercise
    })
    return exercises
  },
  cardsCount (state, getters, rootState, rootGetters) {
    return state.cards.length
  },
  sentencesCount (state, getters, rootState, rootGetters) {
    return state.sentences.length
  },
  estimatedCompleteTime (state, getters, rootState, rootGetters) {
    let totalTime = 0
    getters.cards.forEach(n => {
      if (!n.checked || !n.estimatedCompleteTime) return
      totalTime += n.estimatedCompleteTime
    })
    getters.sentences.forEach(n => {
      if (!n.checked || !n.estimatedCompleteTime) return
      totalTime += n.estimatedCompleteTime
    })
    return totalTime
  },
}

const actions = {
  handleChangeNav ({ commit, state, rootState }, nav) {
    commit(UPDATE_NAV, nav)
  },
  async handleInit ({ commit, state, rootState }) {
    let unitIds = 192
    let cards = await $exercise.getAll({
      unitIds,
      exerciseType: 'FLASH_CARD'
    })
    let sentences = await $exercise.getAll({
      unitIds,
      exerciseType: 'READ_ALOUD'
    })
    commit(UPDATE_CARDS, cards)
    commit(UPDATE_SENTENCES, sentences)
  },
  handleToggleSelect ({ commit, state, rootState }, id) {
    let ids = toggleElFromArr([...state.exerciseSelectIds], id)
    commit(UPDATE_EXERCISE_SELECT_IDS, ids)
  },
  handlePreviewHomeword ({ commit, state, rootState }) {
    wx.navigateTo({
      url: '/pages/homework-preview/main'
    })
  },
}

const mutations = {
  [UPDATE_NAV] (state, nav) {
    state.nav = nav
  },
  [UPDATE_EXERCISE_SELECT_IDS] (state, ids) {
    state.exerciseSelectIds = ids
  },
  [UPDATE_EXERCISES] (state, exercises) {
    state.exercises = exercises
  },
  [UPDATE_CARDS] (state, cards) {
    state.cards = cards
  },
  [UPDATE_SENTENCES] (state, sentences) {
    state.sentences = sentences
  },
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
